package io.sdph.controller;



import io.sdph.userprofile.ISessionManager;
import io.sdph.userprofile.IUserManager;
import io.sdph.userprofile.exceptions.UserProfileException;
import io.sdph.userprofile.model.Address;
import io.sdph.userprofile.model.User;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


/**
 * Servlet odpowiedzialny za dodanie uzytkownika w panelu przedstawiciela handlowego (admin.jsp)
 *
 * @author MODUL FRONTU (WWW)
 */
@Controller
@RequestMapping("/admin/AddUser")
public class AdminAddUser {

        private static final String FIRST_NAME = "firstname";
        private static final String LAST_NAME = "lastname";
        private static final String ADDRESS = "address";
        private static final String POST_CODE = "postcode";
        private static final String CITY = "city";
        private static final String PHONE = "phone";
        private static final String E_MAIL = "email";
        private static final String ANOTHER = "all";        

        private static final String OUTPUT_ERROR = "errors";
        private static final String FIRST_NAME_EMPTY_ERROR = "adminAddUser.firstName.error.empty";
        private static final String LAST_NAME_EMPTY_ERROR = "adminAddUser.lastName.error.empty";
        private static final String ADDRESS_EMPTY_ERROR = "adminAddUser.address.error.empty";
        private static final String POST_CODE_EMPTY_ERROR = "adminAddUser.postCode.error.empty";
        private static final String CITY_EMPTY_ERROR = "adminAddUser.city.error.empty";
        private static final String PHONE_EMPTY_ERROR = "adminAddUser.phone.error.empty";
        private static final String E_MAIL_EMPTY_ERROR = "adminAddUser.eMail.error.empty";
       
        private static final String PAGE_VIEW = "admin";

        /** Komponent pomocniczy do zarz�dzania sesj� u�ytkownika. */
        @Autowired
        ISessionManager sessionManager;
        
        @Autowired
        IUserManager userManager;
       
        //HttpServletRequest arg0, HttpServletResponse arg1
        @RequestMapping(method=RequestMethod.GET)
        public ModelAndView showPage() throws Exception {
               
                ModelAndView model = new ModelAndView(PAGE_VIEW);
                model.addObject("pageName", "user");
                return model;
        }
       
        @RequestMapping(method=RequestMethod.POST)
        public ModelAndView adminAddUser(
                        @RequestParam(FIRST_NAME) final String first_name,
                        @RequestParam(LAST_NAME) final String last_name,
                        @RequestParam(ADDRESS) final String address,
                        @RequestParam(POST_CODE) final String post_code,
                        @RequestParam(CITY) final String city,
                        @RequestParam(PHONE) final String phone,
                        @RequestParam(E_MAIL) final String e_mail                          
        				) {
               
                ModelAndView model = new ModelAndView(PAGE_VIEW);
                
                Map<String, String> errors = new HashMap();
               
                if (StringUtils.isEmpty(first_name)) 
                        errors.put(FIRST_NAME, FIRST_NAME_EMPTY_ERROR);

                if (StringUtils.isEmpty(last_name)) 
                        errors.put(LAST_NAME, LAST_NAME_EMPTY_ERROR);
                
                if (StringUtils.isEmpty(address)) 
                    errors.put(ADDRESS, ADDRESS_EMPTY_ERROR);
               
                if (StringUtils.isEmpty(post_code)) 
                    errors.put(POST_CODE, POST_CODE_EMPTY_ERROR);
               
                if (StringUtils.isEmpty(post_code)) 
                    errors.put(CITY, CITY_EMPTY_ERROR);
               
                if (StringUtils.isEmpty(phone)) 
                    errors.put(PHONE, PHONE_EMPTY_ERROR);
               
                if (StringUtils.isEmpty(post_code)) 
                    errors.put(E_MAIL, E_MAIL_EMPTY_ERROR);
                
               if (errors.isEmpty()) {
            	   User user = new User(null);
            	   user.setAddress(new Address());
            	   user.setFirstName(first_name);
            	   user.setLastName(last_name);
            	   user.setLogin(first_name);
            	   try {
					userManager.addUser(user, "haslo");
				} catch (UserProfileException e) {
					
				}
               } else {
            	   model.addObject(OUTPUT_ERROR, errors);
               }
               
                model.addObject("pageName", "user");
                return model;
        }
       
}
